﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages
{
    public partial class TaskListMoore : BSCMFF.Core.MultiLanguagePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            BSCEntities context = new BSCEntities();

            Task t = new Task();
            t.Name = (userControl.FindControl("TextBoxName") as RadTextBox).Text;
            t.Description = (userControl.FindControl("TextBoxDescription") as RadTextBox).Text;
            t.Weight = Convert.ToInt32((userControl.FindControl("NumericTextBoxWeight") as RadNumericTextBox).Value);
            t.Priority = Convert.ToInt32((userControl.FindControl("NumericTextBoxPriority") as RadNumericTextBox).Value);
            t.Date_Assigned = (userControl.FindControl("RadDatePickerAssigned") as RadDatePicker).SelectedDate;
            t.Date_EndExpected = (userControl.FindControl("RadDatePickerEndExpected") as RadDatePicker).SelectedDate;
            t.User_FK = Convert.ToInt32((userControl.FindControl("ComboBoxUsers") as RadComboBox).SelectedValue);

            context.Tasks.AddObject(t);


            context.SaveChanges();

        }

        protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl taskDetail = (UserControl)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            int task_ID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Task_ID"].ToString());

            BSCEntities context = new BSCEntities();
            var task = (from t in context.Tasks
                        where t.Task_ID == task_ID
                        select t).Single();

            context.DeleteObject(task);
            context.SaveChanges();
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Task_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Task t = (from task in context.Tasks
                      where task.Task_ID == id
                      select task).Single();

            t.Name = (userControl.FindControl("TextBoxName") as RadTextBox).Text;
            t.Description = (userControl.FindControl("TextBoxDescription") as RadTextBox).Text;
            t.Weight = Convert.ToInt32((userControl.FindControl("NumericTextBoxWeight") as RadNumericTextBox).Value);
            t.Priority = Convert.ToInt32((userControl.FindControl("NumericTextBoxPriority") as RadNumericTextBox).Value);
            t.Date_Assigned = (userControl.FindControl("RadDatePickerAssigned") as RadDatePicker).SelectedDate;
            t.Date_EndExpected = (userControl.FindControl("RadDatePickerEndExpected") as RadDatePicker).SelectedDate;
            t.User_FK = Convert.ToInt32((userControl.FindControl("ComboBoxUsers") as RadComboBox).SelectedValue);

            context.SaveChanges();
        }

        
    }
}